package com.cf.controller;

import com.cf.entity.Item;
import com.cf.entity.ItemMan;
import com.cf.entity.Result;
import com.cf.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;

/**
 * @author 59899
 */
@RestController
@CrossOrigin
public class ItemController {

    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemCategoryService itemCategoryService;
    @Autowired
    private ItemManService itemManService;
    @Autowired
    private UserItemService userItemService;
    @Autowired
    private RewardService rewardService;

    @RequestMapping("/addCategoryImg")
    public Object addCategoryImg(HttpServletRequest request, MultipartFile file) throws Exception{
        String fName = file.getOriginalFilename();
        if (fName.contains("/")){
            fName = fName.substring(fName.lastIndexOf("/")+1);
        }
        fName = UUID.randomUUID().toString().replace("-","")+"-"+fName;
        String img = request.getRealPath("img");
        file.transferTo(new File(img,fName));
        return new Result(200,fName,null);
    }

    @RequestMapping("/addMessToItem")
    public Object addMessToItem(Item item, int[] cid, ItemMan itemMan,long userid){
        //向item表中添加信息
        int addItem = itemService.addMessToItem(item);
        //向item-man表中添加信息
        int addItemMan = itemManService.addItemMan(itemMan);

        //先查询item的id
        Item item1 = itemService.selectItemByIname(item.getIname());
        //向user_item表中添加信息
        int addUserItem = userItemService.addUserItem(userid, item1.getIid());

        //判断是否添加成功
        if (addItem!=0 && addItemMan!=0 && addUserItem!=0){
            //根据item的名称查询该对象
            Item selectItem = itemService.selectItemByIname(item.getIname());
            //根据item-man的电话查询该对象
            ItemMan selectItemMan = itemManService.selectItemByTel(itemMan.getMtel());
            //向item-category表中添加关联 (中间表)
            int i = itemCategoryService.addCategory(selectItem.getIid(), cid);
            //向item-man中添加关联 (中间表)
            int j = itemManService.addItemManToItemMan(selectItem.getIid(), selectItemMan.getMid());
            if (i!=0 && j!=0){
                return new Result(200,"添加成功",selectItem);
            }else {
                return new Result(400,"添加失败",null);
            }
        }else {
            return new Result(400,"添加失败",null);
        }
    }

    @RequestMapping("/addStatusToItemById")
    public Object addStatusToItemById(long iid){
        int i = itemService.addStatusToItemById(iid);
        if (i!=0) {
            return new Result(200,"添加成功",null);
        }else {
            return new Result(400,"添加失败",null);
        }
    }


    @RequestMapping("/addTimeToItemById")
    public Object addTimeToItemById(Item item) {
        int i = itemService.addTimeToItemById(item);
        if (i!=0) {
            return new Result(200,"添加成功",null);
        }else {
            return new Result(400,"添加失败",null);
        }
    }

    @RequestMapping("/deleteItemById")
    public Object deleteItemById(long iid) {
        //在item表中删除数据
        int i = itemService.deleteItemById(iid);
        //根据iid查询取其绑定的mid
        int i1 = itemManService.deleteItemManAndManByIid(iid);
        //根据iid删除item-category表中数据
        int i2 = itemCategoryService.deleteCategoryByIid(iid);
        //根据iid删除reward表中数据
        int i3 = rewardService.deleteByIid(iid);
        if (i!=0 && i1!=0 && i2!=0 && i3!=0) {
            return new Result(200,"删除成功",null);
        }else {
            return new Result(400,"删除失败",null);
        }
    }

    //逻辑删除
    @RequestMapping("/deleteLuoJi")
    public Object deleteLuoJi(long iid) {
        int i = itemService.deleteLuoJi(iid);
        if (i!=0) {
            return new Result(200,"删除成功",null);
        }else {
            return new Result(400,"删除失败",null);
        }
    }

    @RequestMapping("/selectAllLikeStatusPage")
    public Object selectAllLikeStatusPage(@RequestParam(defaultValue = "1") int pageNum, String status) {
        PageInfo<Item> itemPageInfo = itemService.selectAllLikeStatusPage(pageNum, status);
        if (itemPageInfo!=null) {
            return new Result(200,"查询成功",itemPageInfo);
        }else {
            return new Result(400,"查询失败",null);
        }
    }

    @RequestMapping("/selectItemByIid")
    public Object selectItemByIid(long iid) {
        Item item = itemService.selectItemByIid(iid);
        if (item!=null) {
            return new Result(200,"查询成功",item);
        }else {
            return new Result(400,"查询失败",null);
        }
    }

    @RequestMapping("/showItemImg")
    public void showImg(String filename, HttpServletRequest request, HttpServletResponse response) throws Exception{
        String img = request.getRealPath("img");
        File file = new File(img, filename);
        FileInputStream fis = new FileInputStream(file);
        response.setHeader("Content-Disposition","filename="+filename);
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(fis,outputStream);
        outputStream.close();
        fis.close();
    }

    @RequestMapping("/updateItemStatusByIid")
    public Object updateItemStatusByIid(long iid) {
        Item item = itemService.selectItemByIid(iid);
        Date iStart =  new Date();
        long iStartTime = iStart.getTime();
        long iEndTime =iStartTime+(item.getIdays()*24*60*60*1000);
        Date iEnd = new Date(iEndTime);
        Map map = new HashMap();
        map.put("istart",iStart);
        map.put("iend",iEnd);
        map.put("iid",iid);
        int i = itemService.updateItemStatusByIid(map);
        if (i!=0) {
            return new Result(200,"审核通过成功",null);
        }else {
            return new Result(400,"审核通过失败",null);
        }
    }

    @RequestMapping("/selectRewardAndItem")
    public Object selectRewardAndItem(long iid){
        Item item = itemService.selectRewardAndItem(iid);
        if (item!=null){
            return new Result(200,"查找成功",item);
        }
        return new Result(400,"没有此数据",0);
    }


    @RequestMapping("/selectItemUserid")
    public Object selectItemUserid(long userid){
        List<Item> items = itemService.selectItemUserid(userid);
        List l1 = new ArrayList();
        List l2 = new ArrayList();
        for (Item item : items) {
            String iname = item.getIname();
            long haveman = item.getHaveman();
            l1.add(iname);
            l2.add(haveman);
        }
        List l3 = new ArrayList();
        l3.add(l1);
        l3.add(l2);
        if (items.size()!=0){
            return new Result(200,"查找成功",l3);
        }
        return new Result(400,"没有此数据",0);
    }

    @RequestMapping("/selectAllItemByPage")
    public Object selectAllItemByPage(@RequestParam(defaultValue = "1")int pageNum) {
        PageInfo<Item> itemPageInfo = itemService.selectAllItemByPage(pageNum);
        if (itemPageInfo.getList().size()!=0) {
            return new Result(200,"查询成功",itemPageInfo);
        }else {
            return new Result(400,"未查询到与条件相符的项目",null);
        }
    }

    @RequestMapping("/selectItemByStatusAndCid")
    public Object selectItemByStatusAndCid(String status, long[] cid, String iname, @RequestParam(defaultValue = "1")int pageNum) {
        Map map = new HashMap();
        map.put("status",status);
        if (cid.length!=0){
            map.put("cid",cid);
        }
        map.put("iname",iname);
        PageInfo<Item> itemPageInfo = itemService.selectItemByStatusAndCid(pageNum, map);
        if (itemPageInfo.getList().size()!=0) {
            return new Result(200,"查询成功",itemPageInfo);
        }else {
            return new Result(400,"未查询到与条件相符的项目",null);
        }
    }

    @RequestMapping("/updateStatusByIid")
    public Object updateStatusByIid(long iid, String notshow) {
        int i = itemService.updateStatusByIid(iid,notshow);
        if (i!=0) {
            return new Result(200,"审核不通过成功",null);
        }else {
            return new Result(400,"审核不通过失败",null);
        }
    }


}
